package com.teamsun.util;

import com.teamsun.rptcfg.BaseRpt;
import com.teamsun.rptcfg.BaseRptColumn;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>工具类</p>
 */
public class CommonUtils {

    /**
     * 判断字符串是否为数字
     * @param str
     * @return
     */
    public static boolean isNumberic(String str) {
        if(StringUtils.isNotBlank(str)) {
            // 如果是03000这种字符串，则判断为不是数字
            if(str.indexOf(".") < 0 && str.startsWith("0")) {
                return false;
            }

            // 该正则表达式可以匹配所有的数字 包括负数
            Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]*");

            String bigdecimalStr = "";
            try {
                bigdecimalStr = new BigDecimal(str).toString();
            } catch (Exception e) {
                return false;
            }

            Matcher isNum = pattern.matcher(bigdecimalStr); // matcher是全匹配

            if(!isNum.matches()) {
                return false;
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * 对于有四则运算的汇总列，需要特殊处理
     * @param columns 所有列信息
     * @param compareField 待比较的field信息
     */
    public static String exactMatch(List<BaseRptColumn> columns, String compareField) {
        compareField = compareField.toLowerCase();

        // 将运算符转换成空格
        compareField = compareField.toLowerCase();
        compareField = compareField.replace("+", " + ");
        compareField = compareField.replace("-", " - ");
        compareField = compareField.replace("*", " * ");
        compareField = compareField.replace("/", " / ");
        compareField = compareField.replace("=", " = ");
        compareField = compareField.replace("(", " ( ");
        compareField = compareField.replace(")", " ) ");

        // 将字符串分隔为单个单词
        String[] words = compareField.split(" ");
        // 单词匹配替换
        for(int i=0; i<words.length; i++) {
            String word = words[i];
            if(word.contains(".")) {
                word = word.substring(word.indexOf(".") + 1);
            }
            for(BaseRptColumn column : columns) {
                String aliasField = column.getAliasField();
                String lowerField = aliasField.toLowerCase();
                if(word.equals(lowerField)) {
                    words[i] = "SUM(\"" + aliasField + "\")";
                    break;
                }
            }
        }

        // 重新组装字符串
        StringBuilder sb = new StringBuilder();
        for(String word : words) {
            sb.append(word).append(" ");
        }
        return sb.toString();
    }

    /**
     * 设置查询条件中日期类型的默认值
     * @param rptColumn 查询列
     */
    public static void setDefaultDate(BaseRptColumn rptColumn) {
        String formatter = rptColumn.getFormatter();
        //String defalutValue = rptColumn.getDefaultValue();
        String defalutValue = rptColumn.getDateDefault();
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatter);
        if(StringUtils.isBlank(defalutValue)) {  // 如果未配置默认值，则设置日期的默认值为当前日期
            String defaultDate = dateFormat.format(new Date());
            rptColumn.setDefaultValue(defaultDate);
            rptColumn.setDefaultValueEnd(defaultDate);
        } else {  // 根据defaultValue的值，获取默认日期
            int dateValue = 0;
            int dateValueEnd = 0;
            String strDateStart = "";
            String strDateEnd = "";
            if(defalutValue.contains(",")) {
                String[] defaults = defalutValue.split(",");
                if(!Constants.CONSTANTS_NULL.equals(defaults[0])) {
                    dateValue = Integer.valueOf(defaults[0]);
                } else {
                    strDateStart = Constants.CONSTANTS_NULL;
                }
                if(!Constants.CONSTANTS_NULL.equals(defaults[1])) {
                    dateValueEnd = Integer.valueOf(defaults[1]);
                } else {
                    strDateEnd = Constants.CONSTANTS_NULL;
                }
            } else {
                if(!Constants.CONSTANTS_NULL.equals(defalutValue)) {
                    dateValue = Integer.valueOf(defalutValue);
                    dateValueEnd = Integer.valueOf(defalutValue);
                } else {
                    strDateStart = Constants.CONSTANTS_NULL;
                    strDateEnd = Constants.CONSTANTS_NULL;
                }
            }
            Calendar calendar = Calendar.getInstance();
            Calendar calendarEnd = Calendar.getInstance();
            if(formatter.contains("dd")) {
                calendar.add(Calendar.DATE, dateValue);
                calendarEnd.add(Calendar.DATE, dateValueEnd);
            } else if(formatter.contains("MM")) {
                calendar.add(Calendar.MONTH, dateValue);
                calendarEnd.add(Calendar.MONTH, dateValueEnd);
            } else if(formatter.contains("yyyy")) {
                calendar.add(Calendar.YEAR, dateValue);
                calendarEnd.add(Calendar.YEAR, dateValueEnd);
            }
            String defaultDate = dateFormat.format(calendar.getTime());
            String defaultDateEnd = dateFormat.format(calendarEnd.getTime());
            if(Constants.CONSTANTS_NULL.equals(strDateStart)) {
                rptColumn.setDefaultValue("");
            } else {
                rptColumn.setDefaultValue(defaultDate);
            }
            if(Constants.CONSTANTS_NULL.equals(strDateEnd)) {
                rptColumn.setDefaultValueEnd("");
            } else {
                rptColumn.setDefaultValueEnd(defaultDateEnd);
            }
        }
    }

    /**
     * 设置查询条件中日期类型的起始默认值
     * @param rptColumn 查询列
     */
    public static void setDefaultDateStart(BaseRptColumn rptColumn) {
        String formatter = rptColumn.getFormatter();
        //String defalutValue = rptColumn.getDefaultValue();
        String defalutValue = rptColumn.getDateDefault();
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatter);
        if(StringUtils.isBlank(defalutValue)) {  // 如果未配置默认值，则设置日期的默认值为当前日期
            String defaultDate = dateFormat.format(new Date());
            rptColumn.setDefaultValue(defaultDate);
        } else {  // 根据defaultValue的值，获取默认日期
            int dateValue = 0;
            int dateValueEnd = 0;
            String strDateStart = "";
            String strDateEnd = "";
            if(defalutValue.contains(",")) {
                String[] defaults = defalutValue.split(",");
                if(!Constants.CONSTANTS_NULL.equals(defaults[0])) {
                    dateValue = Integer.valueOf(defaults[0]);
                } else {
                    strDateStart = Constants.CONSTANTS_NULL;
                }
                if(!Constants.CONSTANTS_NULL.equals(defaults[1])) {
                    dateValueEnd = Integer.valueOf(defaults[1]);
                } else {
                    strDateEnd = Constants.CONSTANTS_NULL;
                }
            } else {
                if(!Constants.CONSTANTS_NULL.equals(defalutValue)) {
                    dateValue = Integer.valueOf(defalutValue);
                    dateValueEnd = Integer.valueOf(defalutValue);
                } else {
                    strDateStart = Constants.CONSTANTS_NULL;
                    strDateEnd = Constants.CONSTANTS_NULL;
                }
            }
            Calendar calendar = Calendar.getInstance();
            Calendar calendarEnd = Calendar.getInstance();
            if(formatter.contains("dd")) {
                calendar.add(Calendar.DATE, dateValue);
                calendarEnd.add(Calendar.DATE, dateValueEnd);
            } else if(formatter.contains("MM")) {
                calendar.add(Calendar.MONTH, dateValue);
                calendarEnd.add(Calendar.MONTH, dateValueEnd);
            } else if(formatter.contains("yyyy")) {
                calendar.add(Calendar.YEAR, dateValue);
                calendarEnd.add(Calendar.YEAR, dateValueEnd);
            }
            String defaultDate = dateFormat.format(calendar.getTime());
            String defaultDateEnd = dateFormat.format(calendarEnd.getTime());
            if(Constants.CONSTANTS_NULL.equals(strDateStart)) {
                rptColumn.setDefaultValue("");
            } else {
                rptColumn.setDefaultValue(defaultDate);
            }
        }
    }

    /**
     * 设置查询条件中日期类型的结束默认值
     * @param rptColumn 查询列
     */
    public static void setDefaultDateEnd(BaseRptColumn rptColumn) {
        String formatter = rptColumn.getFormatter();
        //String defalutValue = rptColumn.getDefaultValue();
        String defalutValue = rptColumn.getDateDefault();
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatter);
        if(StringUtils.isBlank(defalutValue)) {  // 如果未配置默认值，则设置日期的默认值为当前日期
            String defaultDate = dateFormat.format(new Date());
            rptColumn.setDefaultValueEnd(defaultDate);
        } else {  // 根据defaultValue的值，获取默认日期
            int dateValue = 0;
            int dateValueEnd = 0;
            String strDateStart = "";
            String strDateEnd = "";
            if(defalutValue.contains(",")) {
                String[] defaults = defalutValue.split(",");
                if(!Constants.CONSTANTS_NULL.equals(defaults[0])) {
                    dateValue = Integer.valueOf(defaults[0]);
                } else {
                    strDateStart = Constants.CONSTANTS_NULL;
                }
                if(!Constants.CONSTANTS_NULL.equals(defaults[1])) {
                    dateValueEnd = Integer.valueOf(defaults[1]);
                } else {
                    strDateEnd = Constants.CONSTANTS_NULL;
                }
            } else {
                if(!Constants.CONSTANTS_NULL.equals(defalutValue)) {
                    dateValue = Integer.valueOf(defalutValue);
                    dateValueEnd = Integer.valueOf(defalutValue);
                } else {
                    strDateStart = Constants.CONSTANTS_NULL;
                    strDateEnd = Constants.CONSTANTS_NULL;
                }
            }
            Calendar calendar = Calendar.getInstance();
            Calendar calendarEnd = Calendar.getInstance();
            if(formatter.contains("dd")) {
                calendar.add(Calendar.DATE, dateValue);
                calendarEnd.add(Calendar.DATE, dateValueEnd);
            } else if(formatter.contains("MM")) {
                calendar.add(Calendar.MONTH, dateValue);
                calendarEnd.add(Calendar.MONTH, dateValueEnd);
            } else if(formatter.contains("yyyy")) {
                calendar.add(Calendar.YEAR, dateValue);
                calendarEnd.add(Calendar.YEAR, dateValueEnd);
            }
            String defaultDate = dateFormat.format(calendar.getTime());
            String defaultDateEnd = dateFormat.format(calendarEnd.getTime());
            if(Constants.CONSTANTS_NULL.equals(strDateEnd)) {
                rptColumn.setDefaultValueEnd("");
            } else {
                rptColumn.setDefaultValueEnd(defaultDateEnd);
            }
        }
    }
}
